<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" label-width="89px" v-show="showSearch" class="ele-form-search"
             @keyup.enter.native="reload"
             @submit.native.prevent>
      <el-row :gutter="15">
        <el-col :lg="8" :md="12">
          <el-form-item label="产品名称" prop="materialName">
            <el-input
              v-model="queryParams.materialName"
              placeholder="请输入产品名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <!--        <el-col :lg="8" :md="12">-->
        <!--          <el-form-item label="质检结果" prop="zhijians">-->
        <!--            <el-select clearable v-model="queryParams.zhijians" placeholder="请选择质检结果" class="ele-fluid">-->
        <!--              <el-option v-for="item in inspectionResultList" :key="item.type" :label="item.name" :value="item.type" />-->
        <!--            </el-select>-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <el-col :lg="8" :md="12">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['wareManagement:inventoryInquiry:export']"
          >导出
          </el-button>
        </el-col>
        <el-col :lg="8" :md="12">

        </el-col>
      </el-row>

    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      &lt;!&ndash;            <el-col :span="1.5">&ndash;&gt;-->
    <!--      &lt;!&ndash;              <el-button&ndash;&gt;-->
    <!--      &lt;!&ndash;                type="primary"&ndash;&gt;-->
    <!--      &lt;!&ndash;                plain&ndash;&gt;-->
    <!--      &lt;!&ndash;                icon="el-icon-circle-plus"&ndash;&gt;-->
    <!--      &lt;!&ndash;                class="ele-btn-icon" size="small"&ndash;&gt;-->
    <!--      &lt;!&ndash;                @click="batchOutbound"&ndash;&gt;-->
    <!--      &lt;!&ndash;                v-hasPermi="['wareManagement:inventoryInquiry:add']"&ndash;&gt;-->
    <!--      &lt;!&ndash;              >出库&ndash;&gt;-->
    <!--      &lt;!&ndash;              </el-button>&ndash;&gt;-->
    <!--      &lt;!&ndash;            </el-col>&ndash;&gt;-->
    <!--      &lt;!&ndash;      <el-col :span="1.5">&ndash;&gt;-->
    <!--      &lt;!&ndash;        <el-button&ndash;&gt;-->
    <!--      &lt;!&ndash;          type="success"&ndash;&gt;-->
    <!--      &lt;!&ndash;          plain&ndash;&gt;-->
    <!--      &lt;!&ndash;          icon="el-icon-edit"&ndash;&gt;-->
    <!--      &lt;!&ndash;          size="mini"&ndash;&gt;-->
    <!--      &lt;!&ndash;          :disabled="single"&ndash;&gt;-->
    <!--      &lt;!&ndash;          @click="handleUpdate"&ndash;&gt;-->
    <!--      &lt;!&ndash;          v-hasPermi="['wareManagement:inventoryInquiry:edit']"&ndash;&gt;-->
    <!--      &lt;!&ndash;        >修改&ndash;&gt;-->
    <!--      &lt;!&ndash;        </el-button>&ndash;&gt;-->
    <!--      &lt;!&ndash;      </el-col>&ndash;&gt;-->
    <!--      &lt;!&ndash;      <el-col :span="1.5">&ndash;&gt;-->
    <!--      &lt;!&ndash;        <el-button&ndash;&gt;-->
    <!--      &lt;!&ndash;          type="danger"&ndash;&gt;-->
    <!--      &lt;!&ndash;          plain&ndash;&gt;-->
    <!--      &lt;!&ndash;          icon="el-icon-delete"&ndash;&gt;-->
    <!--      &lt;!&ndash;          size="mini"&ndash;&gt;-->
    <!--      &lt;!&ndash;          :disabled="multiple"&ndash;&gt;-->
    <!--      &lt;!&ndash;          @click="handleDelete"&ndash;&gt;-->
    <!--      &lt;!&ndash;          v-hasPermi="['wareManagement:inventoryInquiry:remove']"&ndash;&gt;-->
    <!--      &lt;!&ndash;        >删除&ndash;&gt;-->
    <!--      &lt;!&ndash;        </el-button>&ndash;&gt;-->
    <!--      &lt;!&ndash;      </el-col>&ndash;&gt;-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="warning"-->
    <!--          plain-->
    <!--          icon="el-icon-download"-->
    <!--          size="mini"-->
    <!--          @click="handleExport"-->
    <!--          v-hasPermi="['wareManagement:inventoryInquiry:export']"-->
    <!--        >导出-->
    <!--        </el-button>-->
    <!--      </el-col>-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table v-loading="loading" :data="inventoryInquiryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="库存查询id" align="center" prop="inquiryId" />-->
      <el-table-column label="产品名称" align="center" prop="materialName" width="200" show-overflow-tooltip/>
      <el-table-column label="产品编码" align="center" prop="materialCode"/>

      <!--      <el-table-column label="库位编号" align="center" prop="materialNumber"/>-->
      <!--      <el-table-column label="库区编号" align="center" prop="reservoirNumber"/>-->
      <!--      <el-table-column label="库存数量" align="center" prop="inventoryDifference"/>-->
      <!--      <el-table-column label="出库类型" align="center" prop="libraryType">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ scope.row.shippingType == 4 ? "销售出库" : scope.row.shippingType == 1 ? "领料出库" : scope.row.shippingType == 2 ? '外采出库' :  scope.row.shippingType == 3 ? '退货出库' : " "}}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="单位" align="center" prop="unit"/>
      <!--      <el-table-column label="批次号" align="center" prop="batchNumber"/>-->
      <!--      &lt;!&ndash;      <el-table-column label="库存状态" align="center" prop="stockStatus"/>&ndash;&gt;-->
      <!--      <el-table-column label="生产日期" align="center" prop="productionDate" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.productionDate, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      &lt;!&ndash;      <el-table-column label="有效天数" align="center" prop="validDays"/>&ndash;&gt;-->
      <!--      <el-table-column label="有效期" align="center" prop="validity" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.validity, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="供应商名称" width="100" align="center" prop="supplierName"/>-->
      <!--      <el-table-column label="订单号" align="center" prop="rderNumber"/>-->

      <!--      <el-table-column label="最后更新人" align="center" prop="lastUpdater"/>-->
      <!--      <el-table-column label="最后更新时间" align="center" prop="lastUpdateTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.lastUpdateTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column width="80px" fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wareManagement:inventoryInquiry:edit']"
          >出库
          </el-button>
          <!--                <el-button-->
          <!--                  size="mini"-->
          <!--                  type="text"-->
          <!--                  icon="el-icon-delete"-->
          <!--                  @click="handleDelete(scope.row)"-->
          <!--                  v-hasPermi="['wareManagement:inventoryInquiry:remove']"-->
          <!--                >删除-->
          <!--                </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库存查询对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="formList" :rules="rules" label-width="120px">
        <el-form-item label="产品名称" prop="materialName">
          {{ form.materialName ? form.materialName : "--" }}
        </el-form-item>
        <el-form-item label="产品编码" prop="materialCode">
          {{ form.materialCode ? form.materialCode : "--" }}
        </el-form-item>
        <el-form-item label="库存数量" prop="magnitude">
          {{ form.magnitude ? form.magnitude : "--" }}
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          {{ form.unit ? form.unit : "--" }}
        </el-form-item>
        <el-form-item label="单价" prop="weight">
          {{ form.weight ? form.weight : "--" }}
        </el-form-item>
        <el-form-item label="操作人员:" prop="operator_id">
          {{ user.userName }}
        </el-form-item>
        <!--            <el-form-item label="出库数量:" prop="outbound_quantity">-->
        <!--              <el-input-number v-model="formList.shipmentQuantity" :min="0" :max="form.magnitude"  size="small" />-->
        <!--            </el-form-item>-->
        <el-form-item label="出库数量:" prop="shipmentQuantity">
          <el-input placeholder="请输入出库数量"
                    clearable v-model="formList.shipmentQuantity"/>
        </el-form-item>
        <el-form-item label="出库类型:" prop="shippingType">
          <el-select clearable v-model="formList.shippingType" placeholder="请选择" class="ele-fluid">
            <el-option v-for="item in OutboundstatusList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--批量出货-->
    <batch-occupancy :data="{ currentall: currentall }" @close="closebathOut" v-if="showbathOutbound"
                     :visible.sync="showbathOutbound" @done="reload"/>
  </div>
</template>

<script>
import {
  listInventoryInquiry,
  getInventoryInquiry,
  delInventoryInquiry,
  addInventoryInquiry,
  updateInventoryInquiry
} from "@/api/wareManagement/inventoryInquiry";
import {allOutbound} from "@/api/OutboundManagement/otherOutbound";
import BatchOccupancy from "./batchOutbound.vue"
import {getUserProfile} from "@/api/system/user";
import {addProductionEceipt} from "@/api/InboundManagement/productionEceipt";
import {getDate} from "@/utils/getTime";

export default {
  name: "InventoryInquiry",
  components: {BatchOccupancy},
  data() {
    return {
      // 质检结果
      inspectionResultList: [
        {
          type: 1, name: "待检"
        },
        {
          type: 2, name: "合格"
        },
        {
          type: 3, name: "不合格"
        }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存查询表格数据
      inventoryInquiryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        storageStatus: null,
        materialCode: null,
        materialName: null,
        materialNumber: null,
        reservoirNumber: null,
        nventoryQuantity: null,
        unit: null,
        batchNumber: null,
        stockStatus: null,
        productionDate: null,
        validDays: null,
        validity: null,
        supplierName: null,
        rderNumber: null,
        createUser: null,
        lastUpdater: null,
        lastUpdateTime: null
      },
      // 表单参数
      form: {},
      formList: {
        shippingType: "",
        shipmentQuantity: ""
      },
      // 表格选中数据
      selection: [],
      // 选中的所有id
      currentall: [],
      user: "",
      // 批量出库
      showbathOutbound: false,
      //出库状态
      OutboundstatusList: [
        {name: "领料出库", id: 1},
        // { name: "调拨出库", id: 2 },
        {name: "外采出库", id: 2},
        {name: "退货出库", id: 3},
        {name: "销售出库", id: 4},
      ],
      // 操作人员
      operateList: [
        {name: "操作人A", id: 1},
        {name: "操作人B", id: 2},
        {name: "操作人C", id: 3},
        {name: "操作人D", id: 4},
        {name: "操作人E", id: 5},
      ],
      // 表单校验
      rules: {

        shipmentQuantity: [
          {required: true, message: '出库数量不能为空', trigger: 'blur'}
        ],
        shippingType: [
          {required: true, message: '请选择出库类型', trigger: 'change'}
        ]
      }
    };
  },
  mounted() {
    this.getList();
    this.getUser()
  },
  methods: {
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data;
      });
    },
    reload() {
      this.getList();
    },
    //批量出库
    batchOutbound() {
      //console.log("")
      if (!this.selection.length) {
        this.$modal.msgSuccess({
          message: '请至少选择一条数据',
          type: 'warning',
        });
        return;
      }
      //console.log("8989898989", this.selection)
      // this.currentall = this.selection.map((d) => d.materialId);
      // this.currentall = this.selection.map((d) => d.deliveryId);
      this.currentall = this.selection
      //console.log("this.selection", this.currentall);
      this.showbathOutbound = true;
    },
    // 关闭弹窗
    closebathOut() {
      this.showbathOutbound = false;
    },

    aa(value) {
      this.queryParams.nventoryQuantity = value.replace(/\D/g, '');
    },
    cc(value) {
      this.form.nventoryQuantity = value.replace(/\D/g, '');
    },
    bb(value) {
      this.queryParams.validDays = value.replace(/\D/g, '');
    },
    dd(value) {
      this.form.validDays = value.replace(/\D/g, '');
    },
    /** 查询库存查询列表 */
    getList() {
      this.loading = true;
      // this.queryParams.storageStatus = 2
      this.queryParams.zhijian = this.queryParams.zhijians || "2"
      allOutbound(this.queryParams).then(response => {
        this.inventoryInquiryList = response.rows;
        //console.log(response)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        inquiryId: null,
        materialCode: null,
        materialName: null,
        materialNumber: null,
        reservoirNumber: null,
        nventoryQuantity: null,
        unit: null,
        batchNumber: null,
        stockStatus: null,
        productionDate: null,
        validDays: null,
        validity: null,
        supplierName: null,
        rderNumber: null,
        createUser: null,
        createTime: null,
        lastUpdater: null,
        lastUpdateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      //console.log("9898989", selection)
      this.selection = selection
      this.ids = selection.map(item => item.deliveryId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存查询";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row)
      this.reset();
      // const inquiryId = row.deliveryId || this.ids
      this.form = row
      //console.log("formformfromf", this.form)
      this.open = true;
      this.title = "出库操作";
      // getInventoryInquiry(inquiryId).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "出库操作";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deliveryId != null) {
            this.form.cjdate = getDate()
            this.form.shippingType = this.formList.shippingType
            this.form.shipmentQuantity = this.formList.shipmentQuantity
            this.form.zhijian = 1
            this.form.storageStatus = 2
            this.form.totalAmount = null
            addProductionEceipt(this.form).then(response => {
              this.$modal.msgSuccess("出库成功");
              this.open = false;
              this.formList = {
                shippingType: "",
                shipmentQuantity: ""
              }
              this.getList();
            });
            //console.log( this.form.cjdata)
          } else {
            addInventoryInquiry(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const inquiryIds = row.inquiryId || this.ids;
      this.$modal.confirm('是否确认删除库存查询编号为"' + inquiryIds + '"的数据项？').then(function () {
        return delInventoryInquiry(inquiryIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wareManagement/inventoryInquiry/export', {
        ...this.queryParams
      }, `inventoryInquiry_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
